package vn.com.horoscope.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;

import vn.com.horoscope.ctl.json.JSONObject;
import vn.com.horoscope.ctl.json.XML;
import vn.com.horoscope.entities.ObjectParent;

import com.google.gson.Gson;


import android.util.Log;

/**
 * 
 * @author VietAnh
 *
 */

public class Utilities {
	
	/**
	 * get list star today
	 * @param s
	 * @return int[]
	 */
	public static int[] getValue(String s){
		int val[] = {0,0,0,0} ;
		String a[] = s.split("/5");
		char c[]= new char[5];
		int length = a.length;
		for (int i=0;i<length;i++){
			c[i] = a[i].charAt(a[i].length()-1);
		}
		if (length == 5) {
			for (int i=0;i<4;i++){
				try {
					val[i] = Character.getNumericValue(c[i]);
				} catch(Exception ex){
					Log.d("Error cast to int ", ex.getMessage());
				}
			}
		}
		return val ;
	}
	
	
	/**
	 * Remove a string from other string
	 * @param s
	 * @param remove
	 * @return 
	 */
	public static String removeString(String s , String remove) {
		String result = s ;
		String[] a = s.split(remove);
		if (a.length == ConstantClass.TOTAL_SPLIT) {
			result = a[0].concat(a[2]);
		}
		return result;
	}
	
	
	
	public static String convertToEnglish(int i) {
		String s ="";
		switch (i) {
			case 1: s= ConstantClass.BACHDUONG;
				break;
			case 2: s= ConstantClass.KIMNGUU;
				break;
			case 3: s= ConstantClass.SONGTU;
				break;
			case 4: s= ConstantClass.CUGIAI;
				break;
			case 5: s= ConstantClass.SUTU;
				break;
			case 6: s= ConstantClass.XUNU;
				break;
			case 7: s= ConstantClass.THIENBINH;
				break;
			case 8: s= ConstantClass.BOCAP;
				break;
			case 9: s= ConstantClass.NHANMA;
				break;
			case 10: s= ConstantClass.MAKET;
				break;
			case 11: s= ConstantClass.BAOBINH;
				break;
			case 12: s= ConstantClass.SONGNGU;
				break;
			default : s= ConstantClass.BACHDUONG ;
				break;
		}
		return s ;
	}
	
	
	public static String getCurrentDate(){
		String s ="";
		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH); // Note: zero based!
		int day = now.get(Calendar.DAY_OF_MONTH);
		s=day+"-"+ (month+1) +"-"+year;
		return s;
	}
	
	/**
	 *  parsing to JavaObject
	 * @return
	 */
	@SuppressWarnings("finally")
	public static ObjectParent getData(){
		ObjectParent obj = null;
		String json ;
		try {
			final String str = Utilities.DownloadText(ConstantClass.serviceURL);
			JSONObject xmlJSONObj = null;

			try 
			{
				//convert xml to json object
				xmlJSONObj = XML.toJSONObject(str);
				//convert jsonobject to string
				json = xmlJSONObj.toString(4);
				Gson gson = new Gson();
				obj = gson.fromJson(json, ObjectParent.class);
			}
				catch (vn.com.horoscope.ctl.json.JSONException e) {
				e.printStackTrace();
			}
			
		} catch (Exception e) {
			Log.d("Error", e.getMessage() );
		} finally {
			return obj;
		}
	}
	
	
	/**
	 *  Get data from server
	 * @param URL
	 * @return
	 */
	 public static String DownloadText(String URL)
	    {
	        int BUFFER_SIZE = 2000;
	        InputStream in = null;
	        try 
	        {
	            in = OpenHttpConnection(URL);
	        } 
	        catch (IOException e1) 
	        {
	            e1.printStackTrace();
	            return "";
	        }
	        
	        InputStreamReader isr = new InputStreamReader(in);
	        int charRead;
	        String str = "";
	        char[] inputBuffer = new char[BUFFER_SIZE];          
	        try 
	        {
	            while ((charRead = isr.read(inputBuffer))>0)
	            {                    
	                //---convert the chars to a String---
	                String readString = 
	                    String.copyValueOf(inputBuffer, 0, charRead);                    
	                str += readString;
	                inputBuffer = new char[BUFFER_SIZE];
	            }
	            in.close();
	        } catch (IOException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	            return "";
	        }    
	        return str;        
	    }
	
	
	/**
	 * 
	 * @param urlString
	 * @return
	 * @throws IOException
	 */
	public static InputStream OpenHttpConnection(String urlString) throws IOException
    {
        InputStream in = null;
        int response = -1;
               
        URL url = new URL(urlString); 
        URLConnection conn = url.openConnection();
                 
        if (!(conn instanceof HttpURLConnection))                     
            throw new IOException("Not an HTTP connection");        
        try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();                 
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();                                 
            }                     
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");            
        }
        return in;     
    }	
	
	
	
	public static String splitString(String s , String r) {
		r+= "-/";
		String[] a = s.split(r);
		if (a.length > 0)
		return a[1];
		else return "Miss";
	}

}
